//
//  RenwuDetailViewController.m
//  penzer
//
//  Created by 还带大道 on 2018/11/5.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "RenwuDetailViewController.h"
#import <WebKit/WebKit.h>

@interface RenwuDetailViewController ()<WKNavigationDelegate,WKUIDelegate>
@property (nonatomic,weak) WKWebView *webView;
@property (nonatomic,copy) NSString *task_id;
@end

@implementation RenwuDetailViewController
 
- (instancetype)initWithTask_id:(NSString *)task_id
{
    if(self = [super init]){
        _task_id = task_id;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation:1 andTitle:@"任务详情" andNaviColor:[UIColor whiteColor] andTitleColor:nil];
    WKWebView *webView = [[WKWebView alloc] init];
    webView.navigationDelegate = self;
    webView.UIDelegate = self;
    self.webView = webView;
    [self.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.mas_equalTo(kTopHeight);
    }];
    [self fetchTaskDetail];
}



#pragma mark--
- (void)fetchTaskDetail
{
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live&ac=member&op=task&m=bz_activities&token=87ffec5d746e139250f2da176caff727&ta=detail
     id 任务ID
     
     */
    dict[@"ctrl"] = @"live";
    dict[@"ac"] = @"member";
    dict[@"op"] = @"task";
    dict[@"ta"] = @"detail";
    dict[@"id"] = self.task_id;
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:USER_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.webView loadHTMLString:res[@"data"][@"des"] baseURL:nil];
            });
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        
    }];
}



#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    RZLog(@"----页面开始加载-----");
}


// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
    RZLog(@"----内容开始返回-----");
}


// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    RZLog(@"----页面加载完成-----");
}



// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
{
    RZLog(@"----页面加载失败-----");
}


// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
    RZLog(@"----接收到服务器跳转请求之后调用-----");
}


// 在收到响应后，决定是否跳转,该方法如果不实现，系统默认跳转。如果实现该方法，则需要设置允许跳转，不设置则报错。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
    RZLog(@"----在收到响应后，决定是否跳转 %@-----",navigationResponse.response.URL.absoluteString);
    decisionHandler(WKNavigationResponsePolicyAllow);
}



// 在发送请求之前，决定是否跳转,该方法如果不实现，系统默认跳转。如果实现该方法，则需要设置允许跳转，不设置则报错。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    RZLog(@"----在发送请求之前，决定是否跳转 %@-----",navigationAction.request.URL.absoluteString);
    decisionHandler(WKNavigationActionPolicyAllow);
}


@end
